<?php
$corpid = "wwff50c87cc1badf0a";
$corpsecret = "W57hMJfRxNkGZ1cYcqaujPFSk2ckalfpnLnN7vDHY0o";
$to = $_REQUEST['to'];
$party = isset($_POST['party']) ? $_POST['party'] :'';
$message = $_POST['message'];
$data = array(
   "touser"=>$to,
   "toparty"=> $party,
   "totag"=> "",
   "msgtype"=> "text",
   "agentid"=> "1000002",
   "text"=> array(
       "content"=> urlencode($message)
   ),
   "safe"=>"0"
);
$result = json_encode($data);
$message_body = urldecode($result);
$token_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=".$corpid."&corpsecret=".$corpsecret;
$result = json_decode(curl($token_url));
$message_url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=". $result->access_token;
$succ = curl($message_url, $message_body, 'POST');
echo $succ;



function curl($url, $data="", $method = 'GET') {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL,$url);

        if($method == "POST")
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        //上面两行是为了解决https证书问题，不加的话会报错，所以直接信任证书了。
        $result=curl_exec($ch);
        curl_close($ch);
        return $result;
}
